import { createApp } from 'vue'
import App from './App.vue'
import Axios from 'axios'
import router from './router/router.js'
import ElementPlus from 'element-plus' // 引入element-plus
import 'element-plus/dist/index.css' // 引入element-plus的样式
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
// Axios 挂载
Axios.interceptors.request.use(
  (config) => {
    // 每次发送请求之前判断vuex中是否存在token
    // 如果存在，则统一在http请求的header都加上token，这样后台根据token判断你的登录情况
    // 即使本地存在token，也有可能token是过期的，所以在响应拦截器中要对返回状态进行判断
    if (window.localStorage.getItem('token')) {
      //   const phone = JSON.parse(window.localStorage.getItem('loginUserInfo')).phone
      //   if (phone) {
      const token = window.localStorage.getItem('token')
      let tokens = token.replace(/["]/g, '')
      tokens && (config.headers['X-Access-Token'] = tokens)
      //   }
    }else{
      config.headers['X-Access-Token'] = '123456'
    }
    return config
  },
  (error) => {
    return Promise.error(error)
  }
)

Axios.interceptors.response.use(
    (response) => {
        if (response.data.status === '0') {
            this.$message({
                type: 'error',
                message: response.data.message,
            });
            return Promise.reject(response.data.message);
        } else if (response.config.headers.fileStream) {
            return response;
        } else {
            return response;
        }
    },
    (error) => {
        if (error.response.status === 401) {
            localStorage.clear();
            window.open('/#/Login?showType=1', '_blank');
        }
        return Promise.reject(error);
    }
);

app.config.globalProperties.$http = Axios
app.config.globalProperties.$route = router

app.use(router)
app.use(ElementPlus)

app.mount('#app')
